home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Info / TeachU14 / SAMS / Code / Day04 / airport.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-08  |  3.1 KB  |  112 lines

  1. //---------------------------------------------------------------------------
  2. #include <condefs.h>
  3. #include <iostream.h>
  4. #include <conio.h>
  5. #pragma hdrstop
  6. //---------------------------------------------------------------------------
  7. USERES("..\Airport.res");
  8. USEUNIT("airplane.cpp");
  9. //---------------------------------------------------------------------------
  10. #include "airplane.h"
  11. int getInput(int max);
  12. void getItems(int& speed, int& dir, int& alt);
  13. int main(int, char **)
  14. {
  15.   char returnMsg[100];
  16.   //
  17.   // Set up an array of  Airplanes and create
  18.   // three Airplane objects.
  19.   //
  20.   Airplane* planes[3];
  21.   planes[0] = new Airplane("TWA 1040");
  22.   planes[1] = new Airplane("United Express 749", COMMUTER);
  23.   planes[2] = new Airplane("Cessna 3238T", PRIVATE);
  24.   //
  25.   // Start the loop.
  26.   //
  27.   do {
  28.     int plane, message, speed, altitude, direction;
  29.     speed = altitude = direction = -1;
  30.     //
  31.     // Get a plane to whom a message will be sent.
  32.     // List all of the planes and let the user pick one.
  33.     //
  34.     cout << endl << "Who do you want to send a message to?";
  35.     cout << endl << endl << "0. Quit" <<  endl;
  36.     for (int i=0;i<3;i++)
  37.       cout << (i + 1) << ". " << planes[i]->name << endl;
  38.     //
  39.     // Call the getInput() function to get the plane number.
  40.     //
  41.     plane = getInput(4);
  42.     //
  43.     // If the user chose item 0 then break out of the loop.
  44.     //
  45.     if (plane == -1) break;
  46.     //
  47.     // The plane acknowledges.
  48.     //
  49.     cout << endl << planes[plane]->name << ", roger.";
  50.     cout << endl << endl;
  51.     //
  52.     // Allow the user to choose a message to send.
  53.     //
  54.     cout << "What message do you want to send?" << endl;
  55.     cout << endl << "0. Quit" << endl;;
  56.     cout << "1. State Change" << endl;
  57.     cout << "2. Take Off" << endl;
  58.     cout << "3. Land" << endl;
  59.     cout << "4. Report Status" << endl;
  60.     message = getInput(5);
  61.     //
  62.     // Break out of the loop if the user chose 0.
  63.     //
  64.     if (message == -1) break;
  65.     //
  66.     // If the user chose item 1 then we need to get input
  67.     // for the new speed, direction, and altitude. Call
  68.     // the getItems() function to do that.
  69.     //
  70.     if (message == 0)
  71.       getItems(speed, direction, altitude);
  72.     //
  73.     // Send the plane the message.
  74.     //
  75.     bool goodMsg = planes[plane]->SendMessage(
  76.       message, returnMsg, speed, direction, altitude);
  77.     //
  78.     // Something was wrong with the message
  79.     //
  80.     if (!goodMsg) cout << endl << "Unable to comply.";
  81.     //
  82.     // Display the plane's response.
  83.     //
  84.     cout << endl << returnMsg << endl;
  85.   } while (1);
  86.   //
  87.   // Delete the Airplaine objects.
  88.   //
  89.   for (int i=0;i<3;i++) delete planes[i];
  90. }
  91. int getInput(int max)
  92. {
  93.   int choice;
  94.   do {
  95.     choice = getch();
  96.     choice -= 49;
  97.   } while (choice < -1 || choice > max);
  98.   return choice;
  99. }
  100. void getItems(int& speed, int& dir, int& alt)
  101. {
  102.   cout << endl << "Enter new speed: ";
  103.   getch();
  104.   cin >> speed;
  105.   cout << "Enter new heading: ";
  106.   cin >> dir;
  107.   cout << "Enter new altitude: ";
  108.   cin >> alt;
  109.   cout << endl;
  110. }
  111. //---------------------------------------------------------------------------
  112.